package com.showsu.editor.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.showsu.editor.dao.TblUserMapper;
import com.showsu.editor.entity.doo.User;
import com.showsu.editor.entity.po.TblUser;
import com.showsu.editor.enums.RowFlagEnum;
import com.showsu.editor.enums.UserStatusEnum;
import com.showsu.editor.enums.UserVipEnum;
import com.showsu.editor.enums.VipLevelEnum;
import com.showsu.editor.service.IUserService;
import com.showsu.editor.utils.MD5Utils;
import com.showsu.editor.utils.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: William
 * @Description:
 * @Date: Created in 5:18 下午 2023/3/21
 * @Modified By:
 */
@Service
@Slf4j
public class UserServiceImpl extends ServiceImpl<TblUserMapper, TblUser> implements IUserService {

    @Autowired
    private TblUserMapper tblUserMapper;

    @Override
    @Transactional
    public User save(String mobile, String openId, String password) {
        TblUser tblUser = new TblUser(mobile, MD5Utils.getMD5(password));
        tblUser.setOpenId(openId);
        //设置默认昵称：随机四位字母加手机号后四位
        tblUser.setNick(StringUtil.randomStr(4, 6) + mobile.substring(mobile.length() - 4));
        tblUser.setCreateTime(new Date());
        tblUserMapper.insert(tblUser);
        return tblUser == null ? null : BeanUtil.copyProperties(tblUser, User.class);
    }

    @Override
    public int expiredUser(Long userId) {
        if (userId == null) {
            return 0;
        }
        TblUser tblUser = new TblUser();
        tblUser.setId(userId);
        tblUser.setVipUser(UserVipEnum.EXPIRED.getCode());
        tblUser.setVipLevelCode(VipLevelEnum.FREE.getCode());
        return tblUserMapper.updateById(tblUser);
    }

    @Override
    public User queryByMobile(String mobile) {
        if (StringUtils.isBlank(mobile)) {
            return null;
        }
        TblUser tblUser = tblUserMapper.selectOne(new QueryWrapper<TblUser>().lambda()
                .eq(TblUser::getMobile, mobile)
                .eq(TblUser::getFlag, RowFlagEnum.UnDelete.getCode())
                .eq(TblUser::getStatus, UserStatusEnum.Enable.getCode())
                .orderByDesc(TblUser::getId).last("limit 1"));
        return tblUser == null ? null : BeanUtil.copyProperties(tblUser, User.class);
    }

    @Override
    public User queryByOpenId(String openId) {
        if (StringUtils.isBlank(openId)) {
            return null;
        }
        TblUser tblUser = tblUserMapper.selectOne(new QueryWrapper<TblUser>().lambda()
                .eq(TblUser::getOpenId, openId)
                .eq(TblUser::getFlag, RowFlagEnum.UnDelete.getCode())
                .eq(TblUser::getStatus, UserStatusEnum.Enable.getCode())
                .orderByDesc(TblUser::getId).last("limit 1"));
        return tblUser == null ? null : BeanUtil.copyProperties(tblUser, User.class);
    }

    @Override
    public User queryById(Long userId) {
        if (userId == null) {
            return null;
        }
        TblUser tblUser = tblUserMapper.selectOne(new QueryWrapper<TblUser>().lambda()
                .eq(TblUser::getId, userId)
                .eq(TblUser::getFlag, RowFlagEnum.UnDelete.getCode())
                .eq(TblUser::getStatus, UserStatusEnum.Enable.getCode())
                .last("limit 1"));
        return tblUser == null ? null : BeanUtil.copyProperties(tblUser, User.class);
    }

    @Override
    public List<User> queryExpiredUser() {
        List<TblUser> tblUserList = tblUserMapper.selectList(new QueryWrapper<TblUser>().lambda()
                .eq(TblUser::getVipUser, UserVipEnum.VIP.getCode())
                .lt(TblUser::getVipEndTime, new Date())
                .eq(TblUser::getFlag, RowFlagEnum.UnDelete.getCode())
                .eq(TblUser::getStatus, UserStatusEnum.Enable.getCode()));
        return CollectionUtils.isEmpty(tblUserList) ? null
                : tblUserList.stream().map(tblUser -> { return BeanUtil.copyProperties(tblUser, User.class); }).collect(Collectors.toList());
    }

    @Override
    public List<User> queryUnExpiredVipUser() {
        List<TblUser> tblUserList = tblUserMapper.selectList(new QueryWrapper<TblUser>().lambda()
                .eq(TblUser::getVipUser, UserVipEnum.VIP.getCode())
                .gt(TblUser::getVipEndTime, new Date())
                .eq(TblUser::getFlag, RowFlagEnum.UnDelete.getCode())
                .eq(TblUser::getStatus, UserStatusEnum.Enable.getCode()));
        return CollectionUtils.isEmpty(tblUserList) ? null
                : tblUserList.stream().map(tblUser -> { return BeanUtil.copyProperties(tblUser, User.class); }).collect(Collectors.toList());
    }

}
